package day10.netserver;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;

public class SimpleServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);

        // 循环响应
        while (true) {
            System.out.println("与客户端建立连接,准备响应...");
            //接收客户端
            Socket s = ss.accept();

            try (
                OutputStream os = s.getOutputStream();  // 往 浏览器写内容的输出对象
                ){
                String str = "hello,地球人, 我来自火星...";
                //转成字节数组
                byte[] b = str.getBytes("utf-8");

                // 设置响应头 \r\n\r\n 最后结束一定要写 不写浏览器不认为是结束 的标志
                String head = "HTTP/1.1 200 OK\r\n Content-Type text/html;" +
                        "charset=utf-8\r\n" +
                        "Content-Length:" + b.length + "\r\n\r\n";
                //写内容出去
                os.write(head.getBytes("utf-8"));
                os.write(str.getBytes());
                System.out.println("响应到页面");
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
